package com.hbzhit.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Swagger配置，集成Knife4j
 * @author DING
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    private ApiInfo initApiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("接口API文档")
                .termsOfServiceUrl("https://www.hbzhit.com")
                .version("1.0.0")
                .build();
    }

    private Docket initDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(initApiInfo())
                .select()
                //加了ApiOperation注解的类，才生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build()
                .securitySchemes(security());
    }

    private List<ApiKey> security() {
        return newArrayList(
                new ApiKey("token", "token", "header")
        );
    }

    @Bean
    public Docket createCommonApi() {
        return initDocket().groupName("公共接口").select().paths(PathSelectors.regex(".*/comm/.*")).build();
    }

    @Bean
    public Docket createLoginApi() {
        return initDocket().groupName("登陆相关接口").select().paths(PathSelectors.regex(".*/login/.*")).build();
    }

    @Bean
    public Docket createAdminApi() {
        return initDocket().groupName("系统管理模块").select().apis(RequestHandlerSelectors.basePackage("com.hbzhit.modules.sys.controller")).build();
    }

    @Bean
    public Docket createActApi() {
        return initDocket().groupName("流程引擎模块").select().apis(RequestHandlerSelectors.basePackage("com.hbzhit.modules.act.controller")).build();
    }

    @Bean
    public Docket createCmApi() {
        return initDocket().groupName("内容管理模块").select().apis(RequestHandlerSelectors.basePackage("com.hbzhit.modules.cm.controller")).build();
    }

    @Bean
    public Docket createNmApi() {
        return initDocket().groupName("通知管理模块").select().apis(RequestHandlerSelectors.basePackage("com.hbzhit.modules.nm.controller")).build();
    }

}
